Skip to main content

ObjectPool<T>

Generic implementation of object pooling pattern with predefined pool size limit. The main purpose is that limited number of frequently used objects can be kept in the pool for further recycling.

Notes: 1) it is not the goal to keep all returned objects. Pool is not meant for storage. If there is no space in the pool, extra returned objects will be dropped.

2) it is implied that if object was obtained from a pool, the caller will return it back in a relatively short time. Keeping checked out objects for long durations is ok, but reduces usefulness of pooling. Just new up your own.

Not returning objects to the pool in not detrimental to the pool's work, but is a bad practice. Rationale: If there is no intent for reusing the object, do not use pool - just use "new".

Assembly: ServiceStack.Text.dll
View Source
Declaration
public class ObjectPool<T>
where T : class

Methods

Allocate()

Produces an instance.

Search strategy is a simple linear probing which is chosen for it cache-friendliness. Note that Free will try to store recycled objects close to the start thus statistically reducing how far we will typically search.

View Source
Declaration
public T Allocate()
Returns

<T>

Free(T)

Returns objects to the pool.

Search strategy is a simple linear probing which is chosen for it cache-friendliness. Note that Free will try to store recycled objects close to the start thus statistically reducing how far we will typically search in Allocate.

View Source
Declaration
public void Free(T obj)
Parameters
TypeName
<T>obj

ForgetTrackedObject(T, T)

Removes an object from leak tracking.

This is called when an object is returned to the pool. It may also be explicitly called if an object allocated from the pool is intentionally not being returned to the pool. This can be of use with pooled arrays if the consumer wants to return a larger array to the pool than was originally allocated.

View Source
Declaration
[Conditional("DEBUG")]
public void ForgetTrackedObject(T old, T replacement = null)
Parameters
TypeName
<T>old
<T>replacement